package com.zthz.douxing.sendmessageservice.thirdmessagesender

import com.gexin.rp.sdk.base.IIGtPush
import com.gexin.rp.sdk.base.IPushResult
import com.gexin.rp.sdk.base.impl.SingleMessage
import com.gexin.rp.sdk.base.impl.Target
import com.gexin.rp.sdk.http.IGtPush
import com.gexin.rp.sdk.template.TransmissionTemplate
import com.zthz.douxing.event.UserWillDepartEvent
import com.zthz.douxing.exception.GeTuiSendMessageException
import com.zthz.douxing.msg.GeTuiMessageResult
import com.zthz.douxing.msg.MessageType
import com.zthz.douxing.utils.CommonUtil
import grails.transaction.Transactional

@Transactional
class GeTuiMessageSenderService implements BaseMessageSenderService{

    def grailsApplication
    def config
    private String APPID = ""
    private String APPKEY = ""
    private String MASTERSECRET = ""
    private String API = "http://sdk.open.api.igexin.com/apiex.htm"

    public Map pushMessageToSingle(platform,getuiMessage){

        if(getGeTuiConfig(platform)){
            try {
                IIGtPush push = new IGtPush(API, APPKEY, MASTERSECRET)

                SingleMessage message = new SingleMessage()
                TransmissionTemplate template = new TransmissionTemplate()

                template.setAppId(APPID)
                template.setAppkey(APPKEY)
                template.setTransmissionContent(getuiMessage.content)
                template.setTransmissionType(2)
                message.setData(template)
                message.setOffline(true)
                message.setOfflineExpireTime(72 * 3600 * 1000)//离线有效时间,单位为毫秒,可选

                Target target = new Target()
                target.setAppId(APPID)
                target.setClientId(getuiMessage.clientId)

                IPushResult ret = push.pushMessageToSingle(message, target)
                return ret.getResponse()
            }  catch (Exception e) {
                log.debug(e.getMessage(),e.printStackTrace())
                e.printStackTrace();
            }
        }
    }

    def getGeTuiConfig(platformType){
        def plantform = getXingAppConfig(platformType)
        if(plantform){
            APPID =plantform.getProperty("APPID")
            APPKEY=plantform.getProperty("APPKEY")
            MASTERSECRET= plantform.getProperty("MASTERSECRET")
        }else{
            return null
        }
    }

    def getXingAppConfig(platform){
        if(platform == "0"){
            return grailsApplication.config.getui.ios
        } else if(platform == "1"){
            return grailsApplication.config.getui.android
        } else {
            return null
        }
    }

    @Override
    def getParamFromUserWillDepartEvent4PlanForm(UserWillDepartEvent event,String msgContent,MessageType messageType) {
        return ["platform":CommonUtil.getPlantform2MessageApp(event.platformType),
                "getuiMessage":['content':messageType.name()+"::"+msgContent,'clientId':event.getuiId]]
    }

    @Override
    def send2One(Object obj) {
        try {
            if(obj instanceof Map){
                obj = (Map)obj
                if(obj && obj.get("platform")&&obj.get("getuiMessage"))
                    return new GeTuiMessageResult().getSendStatus(
                            pushMessageToSingle(obj.get("platform"),
                                    obj.get("getuiMessage")).get("result"))
                else
                    return null
            }else{
                return null
            }
        }catch (Exception e){
            throw new GeTuiSendMessageException(e)
            return e.printStackTrace()
            log.error(e.getMessage(),e.printStackTrace())
        }
    }

    @Override
    def send2Many(Map parm) {
        return null
    }
}
